home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / blockframe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  17.5 KB  |  671 lines

  1. /* Get info from stack frames;
  2.    convert between frames, blocks, functions and pc values.
  3.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "symtab.h"
  25. #include "frame.h"
  26. #include "gdbcore.h"
  27. #include "value.h"        /* for read_register */
  28. #include "target.h"        /* for target_has_stack */
  29.  
  30. /* Required by INIT_EXTRA_FRAME_INFO on 88k.  */
  31. #include <setjmp.h>
  32. #include <obstack.h>
  33.  
  34. CORE_ADDR read_pc ();        /* In infcmd.c */
  35.  
  36. /* Start and end of object file containing the entry point.
  37.    STARTUP_FILE_END is the first address of the next file.
  38.    This file is assumed to be a startup file
  39.    and frames with pc's inside it
  40.    are treated as nonexistent.
  41.  
  42.    Setting these variables is necessary so that backtraces do not fly off
  43.    the bottom of the stack.  */
  44. CORE_ADDR startup_file_start;
  45. CORE_ADDR startup_file_end;
  46.  
  47. /* Is ADDR outside the startup file?  Note that if your machine
  48.    has a way to detect the bottom of the stack, there is no need
  49.    to call this function from FRAME_CHAIN_VALID; the reason for
  50.    doing so is that some machines have no way of detecting bottom
  51.    of stack.  */
  52. int
  53. outside_startup_file (addr)
  54.      CORE_ADDR addr;
  55. {
  56.   return !(addr >= startup_file_start && addr < startup_file_end);
  57. }
  58.  
  59. /* Address of innermost stack frame (contents of FP register) */
  60.  
  61. static FRAME current_frame;
  62.  
  63. /*
  64.  * Cache for frame addresses already read by gdb.  Valid only while
  65.  * inferior is stopped.  Control variables for the frame cache should
  66.  * be local to this module.
  67.  */
  68. struct obstack frame_cache_obstack;
  69.  
  70. /* Return the innermost (currently executing) stack frame.  */
  71.  
  72. FRAME
  73. get_current_frame ()
  74. {
  75.   /* We assume its address is kept in a general register;
  76.      param.h says which register.  */
  77.  
  78.   return current_frame;
  79. }
  80.  
  81. void
  82. set_current_frame (frame)
  83.      FRAME frame;
  84. {
  85.   current_frame = frame;
  86. }
  87.  
  88. FRAME
  89. create_new_frame (addr, pc)
  90.      FRAME_ADDR addr;
  91.      CORE_ADDR pc;
  92. {
  93.   struct frame_info *fci;    /* Same type as FRAME */
  94.  
  95.   fci = (struct frame_info *)
  96.     obstack_alloc (&frame_cache_obstack,
  97.            sizeof (struct frame_info));
  98.  
  99.   /* Arbitrary frame */
  100.   fci->next = (struct frame_info *) 0;
  101.   fci->prev = (struct frame_info *) 0;
  102.   fci->frame = addr;
  103.   fci->next_frame = 0;        /* Since arbitrary */
  104.   fci->pc = pc;
  105.  
  106. #ifdef INIT_EXTRA_FRAME_INFO
  107.   INIT_EXTRA_FRAME_INFO (fci);
  108. #endif
  109.  
  110.   return fci;
  111. }
  112.  
  113. /* Return the frame that called FRAME.
  114.    If FRAME is the original frame (it has no caller), return 0.  */
  115.  
  116. FRAME
  117. get_prev_frame (frame)
  118.      FRAME frame;
  119. {
  120.   /* We're allowed to know that FRAME and "struct frame_info *" are
  121.      the same */
  122.   return get_prev_frame_info (frame);
  123. }
  124.  
  125. /* Return the frame that FRAME calls (0 if FRAME is the innermost
  126.    frame).  */
  127.  
  128. FRAME
  129. get_next_frame (frame)
  130.      FRAME frame;
  131. {
  132.   /* We're allowed to know that FRAME and "struct frame_info *" are
  133.      the same */
  134.   return frame->next;
  135. }
  136.  
  137. /*
  138.  * Flush the entire frame cache.
  139.  */
  140. void
  141. flush_cached_frames ()
  142. {
  143.   /* Since we can't really be sure what the first object allocated was */
  144.   obstack_free (&frame_cache_obstack, 0);
  145.   obstack_init (&frame_cache_obstack);
  146.  
  147.   current_frame = (struct frame_info *) 0; /* Invalidate cache */
  148. }
  149.  
  150. /* Flush the frame cache, and start a new one if necessary.  */
  151. void
  152. reinit_frame_cache ()
  153. {
  154.   FRAME fr = current_frame;
  155.   flush_cached_frames ();
  156.   if (fr)
  157.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  158.                       read_pc ()));
  159. }
  160.  
  161. /* Return a structure containing various interesting information
  162.    about a specified stack frame.  */
  163. /* How do I justify including this function?  Well, the FRAME
  164.    identifier format has gone through several changes recently, and
  165.    it's not completely inconceivable that it could happen again.  If
  166.    it does, have this routine around will help */
  167.  
  168. struct frame_info *
  169. get_frame_info (frame)
  170.      FRAME frame;
  171. {
  172.   return frame;
  173. }
  174.  
  175. /* If a machine allows frameless functions, it should define a macro
  176.    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
  177.    frame_info for the frame, and FRAMELESS should be set to nonzero
  178.    if it represents a frameless function invocation.  */
  179.  
  180. /* Return nonzero if the function for this frame has a prologue.  Many
  181.    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
  182.    function.  */
  183.  
  184. int
  185. frameless_look_for_prologue (frame)
  186.      FRAME frame;
  187. {
  188.   CORE_ADDR func_start, after_prologue;
  189.   func_start = (get_pc_function_start (frame->pc) +
  190.         FUNCTION_START_OFFSET);
  191.   if (func_start)
  192.     {
  193.       after_prologue = func_start;
  194. #ifdef SKIP_PROLOGUE_FRAMELESS_P
  195.       /* This is faster, since only care whether there *is* a prologue,
  196.      not how long it is.  */
  197.       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
  198. #else
  199.       SKIP_PROLOGUE (after_prologue);
  200. #endif
  201.       return after_prologue == func_start;
  202.     }
  203.   else
  204.     /* If we can't find the start of the function, we don't really
  205.        know whether the function is frameless, but we should be able
  206.        to get a reasonable (i.e. best we can do under the
  207.        circumstances) backtrace by saying that it isn't.  */
  208.     return 0;
  209. }
  210.  
  211. #if !defined (INIT_FRAME_PC)
  212. #define INIT_FRAME_PC(fromleaf, prev) \
  213.   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
  214.           prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
  215. #endif
  216.  
  217. /* Return a structure containing various interesting information
  218.    about the frame that called NEXT_FRAME.  Returns NULL
  219.    if there is no such frame.  */
  220.  
  221. struct frame_info *
  222. get_prev_frame_info (next_frame)
  223.      FRAME next_frame;
  224. {
  225.   FRAME_ADDR address;
  226.   struct frame_info *prev;
  227.   int fromleaf = 0;
  228.  
  229.   /* If the requested entry is in the cache, return it.
  230.      Otherwise, figure out what the address should be for the entry
  231.      we're about to add to the cache. */
  232.  
  233.   if (!next_frame)
  234.     {
  235.       if (!current_frame)
  236.     {
  237.       error ("You haven't set up a process's stack to examine.");
  238.     }
  239.  
  240.       return current_frame;
  241.     }
  242.  
  243.   /* If we have the prev one, return it */
  244.   if (next_frame->prev)
  245.     return next_frame->prev;
  246.  
  247.   /* On some machines it is possible to call a function without
  248.      setting up a stack frame for it.  On these machines, we
  249.      define this macro to take two args; a frameinfo pointer
  250.      identifying a frame and a variable to set or clear if it is
  251.      or isn't leafless.  */
  252. #ifdef FRAMELESS_FUNCTION_INVOCATION
  253.   /* Still don't want to worry about this except on the innermost
  254.      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
  255.      frameless function invocation.  */
  256.   if (!(next_frame->next))
  257.     {
  258.       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
  259.       if (fromleaf)
  260.     address = next_frame->frame;
  261.     }
  262. #endif
  263.  
  264.   if (!fromleaf)
  265.     {
  266.       /* Two macros defined in tm.h specify the machine-dependent
  267.      actions to be performed here.
  268.      First, get the frame's chain-pointer.
  269.      If that is zero, the frame is the outermost frame or a leaf
  270.      called by the outermost frame.  This means that if start
  271.      calls main without a frame, we'll return 0 (which is fine
  272.      anyway).
  273.  
  274.      Nope; there's a problem.  This also returns when the current
  275.      routine is a leaf of main.  This is unacceptable.  We move
  276.      this to after the ffi test; I'd rather have backtraces from
  277.      start go curfluy than have an abort called from main not show
  278.      main.  */
  279.       address = FRAME_CHAIN (next_frame);
  280.       if (!FRAME_CHAIN_VALID (address, next_frame))
  281.     return 0;
  282.       address = FRAME_CHAIN_COMBINE (address, next_frame);
  283.     }
  284.   if (address == 0)
  285.     return 0;
  286.  
  287.   prev = (struct frame_info *)
  288.     obstack_alloc (&frame_cache_obstack,
  289.            sizeof (struct frame_info));
  290.  
  291.   if (next_frame)
  292.     next_frame->prev = prev;
  293.   prev->next = next_frame;
  294.   prev->prev = (struct frame_info *) 0;
  295.   prev->frame = address;
  296.   prev->next_frame = prev->next ? prev->next->frame : 0;
  297.  
  298. #ifdef INIT_EXTRA_FRAME_INFO
  299.   INIT_EXTRA_FRAME_INFO(prev);
  300. #endif
  301.  
  302.   /* This entry is in the frame queue now, which is good since
  303.      FRAME_SAVED_PC may use that queue to figure out it's value
  304.      (see m-sparc.h).  We want the pc saved in the inferior frame. */
  305.   INIT_FRAME_PC(fromleaf, prev);
  306.  
  307.   return prev;
  308. }
  309.  
  310. CORE_ADDR
  311. get_frame_pc (frame)
  312.      FRAME frame;
  313. {
  314.   struct frame_info *fi;
  315.   fi = get_frame_info (frame);
  316.   return fi->pc;
  317. }
  318.  
  319. #if defined (FRAME_FIND_SAVED_REGS)
  320. /* Find the addresses in which registers are saved in FRAME.  */
  321.  
  322. void
  323. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  324.      struct frame_info *frame_info_addr;
  325.      struct frame_saved_regs *saved_regs_addr;
  326. {
  327.   FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
  328. }
  329. #endif
  330.  
  331. /* Return the innermost lexical block in execution
  332.    in a specified stack frame.  The frame address is assumed valid.  */
  333.  
  334. struct block *
  335. get_frame_block (frame)
  336.      FRAME frame;
  337. {
  338.   struct frame_info *fi;
  339.   CORE_ADDR pc;
  340.  
  341.   fi = get_frame_info (frame);
  342.  
  343.   pc = fi->pc;
  344.   if (fi->next_frame != 0)
  345.     /* We are not in the innermost frame.  We need to subtract one to
  346.        get the correct block, in case the call instruction was the
  347.        last instruction of the block.  If there are any machines on
  348.        which the saved pc does not point to after the call insn, we
  349.        probably want to make fi->pc point after the call insn anyway.  */
  350.     --pc;
  351.   return block_for_pc (pc);
  352. }
  353.  
  354. struct block *
  355. get_current_block ()
  356. {
  357.   return block_for_pc (read_pc ());
  358. }
  359.  
  360. CORE_ADDR
  361. get_pc_function_start (pc)
  362.      CORE_ADDR pc;
  363. {
  364.   register struct block *bl = block_for_pc (pc);
  365.   register struct symbol *symbol;
  366.   if (bl == 0 || (symbol = block_function (bl)) == 0)
  367.     {
  368.       register int misc_index = find_pc_misc_function (pc);
  369.       if (misc_index >= 0)
  370.     return misc_function_vector[misc_index].address;
  371.       return 0;
  372.     }
  373.   bl = SYMBOL_BLOCK_VALUE (symbol);
  374.   return BLOCK_START (bl);
  375. }
  376.  
  377. /* Return the symbol for the function executing in frame FRAME.  */
  378.  
  379. struct symbol *
  380. get_frame_function (frame)
  381.      FRAME frame;
  382. {
  383.   register struct block *bl = get_frame_block (frame);
  384.   if (bl == 0)
  385.     return 0;
  386.   return block_function (bl);
  387. }
  388.  
  389. /* Return the blockvector immediately containing the innermost lexical block
  390.    containing the specified pc value, or 0 if there is none.
  391.    PINDEX is a pointer to the index value of the block.  If PINDEX
  392.    is NULL, we don't pass this information back to the caller.  */
  393.  
  394. struct blockvector *
  395. blockvector_for_pc (pc, pindex)
  396.      register CORE_ADDR pc;
  397.      int *pindex;
  398. {
  399.   register struct block *b;
  400.   register int bot, top, half;
  401.   register struct symtab *s;
  402.   struct blockvector *bl;
  403.  
  404.   /* First search all symtabs for one whose file contains our pc */
  405.   s = find_pc_symtab (pc);
  406.   if (s == 0)
  407.     return 0;
  408.  
  409.   bl = BLOCKVECTOR (s);
  410.   b = BLOCKVECTOR_BLOCK (bl, 0);
  411.  
  412.   /* Then search that symtab for the smallest block that wins.  */
  413.   /* Use binary search to find the last block that starts before PC.  */
  414.  
  415.   bot = 0;
  416.   top = BLOCKVECTOR_NBLOCKS (bl);
  417.  
  418.   while (top - bot > 1)
  419.     {
  420.       half = (top - bot + 1) >> 1;
  421.       b = BLOCKVECTOR_BLOCK (bl, bot + half);
  422.       if (BLOCK_START (b) <= pc)
  423.     bot += half;
  424.       else
  425.     top = bot + half;
  426.     }
  427.  
  428.   /* Now search backward for a block that ends after PC.  */
  429.  
  430.   while (bot >= 0)
  431.     {
  432.       b = BLOCKVECTOR_BLOCK (bl, bot);
  433.       if (BLOCK_END (b) > pc)
  434.     {
  435.       if (pindex)
  436.         *pindex = bot;
  437.       return bl;
  438.     }
  439.       bot--;
  440.     }
  441.  
  442.   return 0;
  443. }
  444.  
  445. /* Return the innermost lexical block containing the specified pc value,
  446.    or 0 if there is none.  */
  447.  
  448. struct block *
  449. block_for_pc (pc)
  450.      register CORE_ADDR pc;
  451. {
  452.   register struct blockvector *bl;
  453.   int index;
  454.  
  455.   bl = blockvector_for_pc (pc, &index);
  456.   if (bl)
  457.     return BLOCKVECTOR_BLOCK (bl, index);
  458.   return 0;
  459. }
  460.  
  461. /* Return the function containing pc value PC.
  462.    Returns 0 if function is not known.  */
  463.  
  464. struct symbol *
  465. find_pc_function (pc)
  466.      CORE_ADDR pc;
  467. {
  468.   register struct block *b = block_for_pc (pc);
  469.   if (b == 0)
  470.     return 0;
  471.   return block_function (b);
  472. }
  473.  
  474. /* These variables are used to cache the most recent result
  475.  * of find_pc_partial_function. */
  476.  
  477. static CORE_ADDR cache_pc_function_low = 0;
  478. static CORE_ADDR cache_pc_function_high = 0;
  479. static char *cache_pc_function_name = 0;
  480.  
  481. /* Clear cache, e.g. when symbol table is discarded. */
  482.  
  483. void
  484. clear_pc_function_cache()
  485. {
  486.   cache_pc_function_low = 0;
  487.   cache_pc_function_high = 0;
  488.   cache_pc_function_name = (char *)0;
  489. }
  490.  
  491. /* Finds the "function" (text symbol) that is smaller than PC
  492.    but greatest of all of the potential text symbols.  Sets
  493.    *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
  494.    Returns 0 if it couldn't find anything, 1 if it did.  On a zero
  495.    return, *NAME and *ADDRESS are always set to zero.  On a 1 return,
  496.    *NAME and *ADDRESS contain real information.  */
  497.  
  498. int
  499. find_pc_partial_function (pc, name, address)
  500.      CORE_ADDR pc;
  501.      char **name;
  502.      CORE_ADDR *address;
  503. {
  504.   struct partial_symtab *pst;
  505.   struct symbol *f;
  506.   int miscfunc;
  507.   struct partial_symbol *psb;
  508.  
  509.   if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
  510.     {
  511.     if (address)
  512.         *address = cache_pc_function_low;
  513.     if (name)
  514.         *name = cache_pc_function_name;
  515.     return 1;
  516.     }
  517.  
  518.   pst = find_pc_psymtab (pc);
  519.   if (pst)
  520.     {
  521.       if (pst->readin)
  522.     {
  523.       /* The information we want has already been read in.
  524.          We can go to the already readin symbols and we'll get
  525.          the best possible answer.  */
  526.       f = find_pc_function (pc);
  527.       if (!f)
  528.         {
  529.         return_error:
  530.           /* No available symbol.  */
  531.           if (name != 0)
  532.         *name = 0;
  533.           if (address != 0)
  534.         *address = 0;
  535.           return 0;
  536.         }
  537.  
  538.       cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  539.       cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
  540.       cache_pc_function_name = SYMBOL_NAME (f);
  541.       if (name)
  542.         *name = cache_pc_function_name;
  543.       if (address)
  544.         *address = cache_pc_function_low;
  545.       return 1;
  546.     }
  547.  
  548.       /* Get the information from a combination of the pst
  549.      (static symbols), and the misc function vector (extern
  550.      symbols).  */
  551.       miscfunc = find_pc_misc_function (pc);
  552.       psb = find_pc_psymbol (pst, pc);
  553.  
  554.       if (!psb && miscfunc == -1)
  555.     {
  556.       goto return_error;
  557.     }
  558.       if (psb
  559.       && (miscfunc == -1
  560.           || (SYMBOL_VALUE_ADDRESS (psb)
  561.           >= misc_function_vector[miscfunc].address)))
  562.     {
  563.       /* This case isn't being cached currently. */
  564.       if (address)
  565.         *address = SYMBOL_VALUE_ADDRESS (psb);
  566.       if (name)
  567.         *name = SYMBOL_NAME (psb);
  568.       return 1;
  569.     }
  570.     }
  571.   else
  572.     /* Must be in the misc function stuff.  */
  573.     {
  574.       miscfunc = find_pc_misc_function (pc);
  575.       if (miscfunc == -1)
  576.     goto return_error;
  577.     }
  578.  
  579.   {
  580.     if (misc_function_vector[miscfunc].type == mf_text)
  581.       cache_pc_function_low = misc_function_vector[miscfunc].address;
  582.     else
  583.       /* It is a transfer table for Sun shared libraries.  */
  584.       cache_pc_function_low = pc - FUNCTION_START_OFFSET;
  585.   }
  586.   cache_pc_function_name = misc_function_vector[miscfunc].name;
  587.   if (miscfunc < misc_function_count /* && FIXME mf_text again? */ )
  588.     cache_pc_function_high = misc_function_vector[miscfunc+1].address;
  589.   else
  590.     cache_pc_function_high = cache_pc_function_low + 1;
  591.   if (address)
  592.     *address = cache_pc_function_low;
  593.   if (name)
  594.     *name = cache_pc_function_name;
  595.   return 1;
  596. }
  597.  
  598. /* Find the misc function whose address is the largest
  599.    while being less than PC.  Return its index in misc_function_vector.
  600.    Returns -1 if PC is not in suitable range.  */
  601.  
  602. int
  603. find_pc_misc_function (pc)
  604.      register CORE_ADDR pc;
  605. {
  606.   register int lo = 0;
  607.   register int hi = misc_function_count-1;
  608.   register int new;
  609.  
  610.   /* Note that the last thing in the vector is always _etext.  */
  611.   /* Actually, "end", now that non-functions
  612.      go on the misc_function_vector.  */
  613.  
  614.   /* Above statement is not *always* true - fix for case where there are */
  615.   /* no misc functions at all (ie no symbol table has been read). */
  616.   if (hi < 0) return -1;        /* no misc functions recorded */
  617.  
  618.   /* trivial reject range test */
  619.   if (pc < misc_function_vector[0].address ||
  620.       pc > misc_function_vector[hi].address)
  621.     return -1;
  622.  
  623.   /* Note that the following search will not return hi if
  624.      pc == misc_function_vector[hi].address.  If "end" points to the
  625.      first unused location, this is correct and the above test
  626.      simply needs to be changed to
  627.      "pc >= misc_function_vector[hi].address".  */
  628.   do {
  629.     new = (lo + hi) >> 1;
  630.     if (misc_function_vector[new].address == pc)
  631.       return new;        /* an exact match */
  632.     else if (misc_function_vector[new].address > pc)
  633.       hi = new;
  634.     else
  635.       lo = new;
  636.   } while (hi-lo != 1);
  637.  
  638.   /* if here, we had no exact match, so return the lower choice */
  639.   return lo;
  640. }
  641.  
  642. /* Return the innermost stack frame executing inside of the specified block,
  643.    or zero if there is no such frame.  */
  644.  
  645. FRAME
  646. block_innermost_frame (block)
  647.      struct block *block;
  648. {
  649.   struct frame_info *fi;
  650.   register FRAME frame;
  651.   register CORE_ADDR start = BLOCK_START (block);
  652.   register CORE_ADDR end = BLOCK_END (block);
  653.  
  654.   frame = 0;
  655.   while (1)
  656.     {
  657.       frame = get_prev_frame (frame);
  658.       if (frame == 0)
  659.     return 0;
  660.       fi = get_frame_info (frame);
  661.       if (fi->pc >= start && fi->pc < end)
  662.     return frame;
  663.     }
  664. }
  665.  
  666. void
  667. _initialize_blockframe ()
  668. {
  669.   obstack_init (&frame_cache_obstack);
  670. }
  671.